// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-01-08

package types

// Verification is a type name used to define verify result.
type Verification int

const (
	VerificationUnavailable Verification = iota
	VerificationAccessible
	VerificationAuthorized
	VerificationUnauthorized
	VerificationResourceNotFound
	VerificationApplicationNotFound
)

var (
	verificationErrors = map[Verification]error{
		VerificationUnavailable:         ErrUnavailable,
		VerificationUnauthorized:        ErrUnauthorized,
		VerificationResourceNotFound:    ErrResourceNotFound,
		VerificationApplicationNotFound: ErrApplicationNotFound,
	}

	verificationText = map[Verification]string{
		VerificationUnavailable:         ErrUnavailable.Message(),
		VerificationUnauthorized:        ErrUnauthorized.Message(),
		VerificationResourceNotFound:    ErrResourceNotFound.Message(),
		VerificationApplicationNotFound: ErrApplicationNotFound.Message(),

		VerificationAccessible: `publish resource`,
		VerificationAuthorized: `authorize success`,
	}

	verificationTextUnknown = "unknown"
)

// Error returns error for verify result. Return nil if verification is success.
func (o Verification) Error() error {
	if err, ok := verificationErrors[o]; ok {
		return err
	}
	return nil
}

// String returns verification text for verify result.
func (o Verification) String() string {
	if str, ok := verificationText[o]; ok {
		return str
	}
	return verificationTextUnknown
}

// Success returns true if verification for verify is success.
func (o Verification) Success() bool {
	return o == VerificationAccessible || o == VerificationAuthorized
}
